package com.fattyCorps.common.packet.hero.component.prop.component;

import cn.hutool.extra.spring.SpringUtil;
import com.fattyCorps.common.enums.battle.EBehaviorEffectType;
import com.fattyCorps.common.enums.battle.EBehaviorType;
import com.fattyCorps.common.enums.battle.calc.ECalcType;
import com.fattyCorps.common.enums.battle.prop.EStaticProp;
import com.fattyCorps.common.mgr.Cfg;
import com.fattyCorps.common.obj.config.BehaviorEffectMould;
import com.fattyCorps.common.obj.config.HeroMould;
import com.fattyCorps.common.obj.config.SkillBehaviorMould;
import com.fattyCorps.common.util.GameCfgUtils;
import com.fattyCorps.common.packet.hero.Hero;
import com.fattyCorps.common.packet.hero.component.Component;
import com.fattyCorps.common.packet.hero.component.prop.PropAdd;
import com.fattyCorps.common.packet.hero.component.prop.HeroProp;
import lombok.Getter;

import java.util.ArrayList;
import java.util.List;

public abstract class ABasePropComponent implements Component.IComponent {
    @Getter
    HeroProp owner;
    Hero hero;
    HeroMould heroMould;

    Cfg cfg;

    @Override
    public void onCreate(Object owner) {
        this.owner = (HeroProp)owner;
        hero = this.getOwner().getOwner();

        cfg = SpringUtil.getBean(Cfg.class);
        heroMould = cfg.getCfg(HeroMould.class, hero.getTypeId());
    }

    protected List<PropAdd> behaviorPropAdds(List<Integer> bhvIds) {
        List<PropAdd> adds = new ArrayList<>();

        for (Integer bhvId : bhvIds) {
            SkillBehaviorMould bhvMould = cfg.getCfg(SkillBehaviorMould.class, bhvId);

            if (bhvMould.getType().equals(EBehaviorType.PassiveAddProp)) {
                List<Integer> effectIds = GameCfgUtils.splitIdListStr(bhvMould.getEffectIds());

                for (Integer effectId : effectIds) {

                    BehaviorEffectMould effectMould = cfg.getCfg(BehaviorEffectMould.class, effectId);

                    if (effectMould.getType().equals(EBehaviorEffectType.StaticPropPermanent)) {
                        EStaticProp propType = effectMould.getStaticCalcProp();
                        ECalcType calcType = effectMould.getStaticCalcType();
                        PropAdd add = new PropAdd();
                        add.value = effectMould.getStaticCalcValue();
                        add.calcType = calcType;
                        add.propType = propType;
                        adds.add(add);
                    }
                }
            }
        }

        return adds;
    }
}
